home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / HorizontalSlider.java < prev    next >
Text File  |  1998-08-21  |  12KB  |  416 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Color;
  5. import java.awt.Event;
  6. import java.beans.PropertyVetoException;
  7. import java.beans.PropertyChangeListener;
  8. import java.beans.VetoableChangeListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseMotionListener;
  12.  
  13. //     02/15/97    RKM    Added validate - Fixes problem where calls to setMinValue & setMaxValue
  14. //                    would not work if called after reshape
  15. //     02/27/97    RKM    Merged in Scott's change to use the background color of the component
  16. //    05/29/97    MSH    Updated to support Java 1.1
  17. //  06/11/97    MSH Removed postEvent() and added correct listener registration fuctions
  18. //    07/17/97    LAB    Added add/removeNotify for event listener registration.  Separated event handling
  19. //                    innerclass into two innerclasses.  Made the bound and constrained listener
  20. //                    registration functions call it's super.  Changed setTickStyle to call it's super.
  21. //                    Moved doMove() into Slider since it was the same as HorizontalSlider:doMove().
  22. //                    Moved Action Listener/Event stuff into the base class. In general, moved common
  23. //                    things to the base class.
  24. //  07/24/97    CAR marked field transient as needed
  25. //                  implemented readObject
  26. //  08/12/97    RKM Removed property changed and veto support - it was all moved into the
  27. //                    base class and was no longer accessed from this class
  28. //                    Remove unnecessary clipRect in paint call
  29. //    10/05/97    LAB    Made calculations in do_reshape use floats instead of ints to avoid rounding
  30. //                    errors with large numbers of ticks (Addresses Mac Bug #7591).
  31.  
  32. /**
  33.  * This component is used to select one value
  34.  * from a continuous range of values. It has a movable thumb in front of a
  35.  * gauge with ticks marks on it.
  36.  * <p>
  37.  * @see symantec.itools.awt.VerticalSlider
  38.  * @version 1.1, July 17, 1997
  39.  * @author Symantec
  40.  */
  41. public class HorizontalSlider extends Slider
  42. {
  43.     /**
  44.      * Constructs a default HorizontalSlider. The ticks
  45.      * are drawn on both sides of the gauge with a frequency of 1.
  46.      * The minimum value is 1. The maximum value is 10. The
  47.      * border is shown.
  48.      */
  49.     public HorizontalSlider()
  50.     {
  51.         this.thumb    = new HorizontalSliderThumbBoth();
  52.         width        = 200;
  53.     }
  54.  
  55.     /**
  56.      * Sets the current slider tick mark style.
  57.      * @param style the new tick mark style, one of TICK_TOP, TICK_BOTTOM, or
  58.      * TICK_BOTH
  59.      * @see symantec.itools.awt.Slider#getTickStyle
  60.      * @see Slider#TICK_TOP
  61.      * @see Slider#TICK_BOTTOM
  62.      * @see Slider#TICK_BOTH
  63.      * @exception PropertyVetoException
  64.      * if the specified property value is unacceptable
  65.      */
  66.     public void setTickStyle(int style) throws PropertyVetoException
  67.     {
  68.         if (this.style != style)
  69.         {
  70.             super.setTickStyle(style);
  71.             
  72.             switch (style)
  73.             {
  74.                 case TICK_TOP :
  75.                     thumb = new HorizontalSliderThumbTop();
  76.                     break;
  77.                 case TICK_BOTTOM :
  78.                     thumb = new HorizontalSliderThumbBot();
  79.                     break;
  80.                 default :
  81.                     thumb = new HorizontalSliderThumbBoth();
  82.                     break;
  83.             }
  84.             
  85.             repaint();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Tells this component that it has been added to a container.
  91.      * This is a standard Java AWT method which gets called by the AWT when
  92.      * this component is added to a container. Typically, it is used to
  93.      * create this component's peer.
  94.      *
  95.      * It has been overridden here to handle registering event listeners.
  96.      *
  97.      * @see #removeNotify
  98.      */
  99.     public synchronized void addNotify()
  100.     {
  101.         super.addNotify();
  102.  
  103.         //Hook up listeners
  104.         if (mouse == null)
  105.         {
  106.             mouse = new Mouse();
  107.             addMouseListener(mouse);
  108.         }
  109.         if (mouseMotion == null)
  110.         {
  111.             mouseMotion = new MouseMtn();
  112.             addMouseMotionListener(mouseMotion);
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Tells this component that it is being removed from a container.
  118.      * This is a standard Java AWT method which gets called by the AWT when
  119.      * this component is removed from a container. Typically, it is used to
  120.      * destroy the peers of this component and all its subcomponents.
  121.      *
  122.      * It has been overridden here to handle registering event listeners.
  123.      *
  124.      * @see #addNotify
  125.      */
  126.     public synchronized void removeNotify()
  127.     {
  128.         //Unhook listeners
  129.         if (mouse != null)
  130.         {
  131.             removeMouseListener(mouse);
  132.             mouse = null;
  133.         }
  134.         if (mouseMotion != null)
  135.         {
  136.             removeMouseMotionListener(mouseMotion);
  137.             mouseMotion = null;
  138.         }
  139.  
  140.         super.removeNotify();
  141.     }
  142.  
  143.     /**
  144.      * This is the Mouse Event handling innerclass.
  145.      */
  146.     class Mouse extends java.awt.event.MouseAdapter implements java.io.Serializable
  147.     {
  148.         /**
  149.          * Handles Mouse Pressed events
  150.          * @param e the MouseEvent
  151.          */
  152.         public void mousePressed(MouseEvent e)
  153.         {
  154.             moveThumb( e.getX(), true );
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * This is the MouseMotion Event handling innerclass.
  160.      */
  161.     class MouseMtn extends java.awt.event.MouseMotionAdapter implements java.io.Serializable
  162.     {
  163.         /**
  164.          * Handles Mouse Dragged events
  165.          * @param e the MouseEvent
  166.          */
  167.         public void mouseDragged(MouseEvent e)
  168.         {
  169.             moveThumb( e.getX(), false );
  170.         }
  171.     }
  172.  
  173.     protected void do_reshape(int w, int h)
  174.     {
  175.         float hb = BORDER_X;
  176.         float vb = BORDER_Y;
  177.  
  178.         if (w < hb)
  179.             hb = w / 4;
  180.         if (h < vb)
  181.             vb = h / 4;
  182.  
  183.         float x0 = hb;
  184.         float x1 = w - hb;
  185.         float y0 = vb;
  186.         float y1 = h - vb;
  187.  
  188.         if (x0 == 0)
  189.             x0 = 1;
  190.  
  191.         if (x1 == 0)
  192.             x1 = 1;
  193.  
  194.         if (y0 == 0)
  195.             y0 = 1;
  196.  
  197.         if (y1 == 0)
  198.             y1 = 1;
  199.  
  200.         float n = (max - min) / freq + 1;
  201.  
  202.         tick = new SliderTick[(int)n];
  203.  
  204.         float hs = (x1 - x0) / (n - 1), ch;
  205.  
  206.         for (int i = 0; i < n; ++i)
  207.         {
  208.             ch = i * hs;
  209.             tick[i] = new SliderTick((int)y0, (int)y1, (int)(x0 + ch), (int)ch);
  210.         }
  211.  
  212.         thumb.resize((int)(hs / 2), (int)(y1 - y0 - TICK_HEIGHT - 1));
  213.     }
  214.  
  215.     /**
  216.      * Paints this component using the given graphics context.
  217.      * This is a standard Java AWT method which typically gets called
  218.      * by the AWT to handle painting this component. It paints this component
  219.      * using the given graphics context. The graphics context clipping region
  220.      * is set to the bounding rectangle of this component and its [0,0]
  221.      * coordinate is this component's top-left corner.
  222.      *
  223.      * @param g the graphics context used for painting
  224.      * @see java.awt.Component#repaint
  225.      * @see java.awt.Component#update
  226.      */
  227.     public void paint(Graphics g)
  228.     {
  229.         super.paint(g);
  230.         
  231.         if (tick.length == 0)
  232.             return;
  233.  
  234.         g.setColor(getBackground());
  235.         g.fillRect(0, 0, width, height);
  236.  
  237.         g.setColor(Color.black);
  238.  
  239.         int x0, x1, y, w = width - 1, h = height - 1;
  240.         boolean end;
  241.  
  242.         if (showBorder)
  243.             g.drawRect(0, 0, w, h);
  244.  
  245.         for (int i = 0; i < tick.length; ++i)
  246.         {
  247.             end = i == 0 || i == tick.length - 1;
  248.  
  249.             SliderTick t = tick[i];
  250.  
  251.             if (style == TICK_TOP || style == TICK_BOTH)
  252.                 g.drawLine(t.c, t.a + (end ? 0 : 1), t.c, t.a + TICK_HEIGHT);
  253.  
  254.             if (style == TICK_BOTTOM || style == TICK_BOTH)
  255.                 g.drawLine(t.c, t.b - TICK_HEIGHT, t.c, t.b - (end ? 0 : 1));
  256.         }
  257.  
  258.         SliderTick t = tick[0];
  259.         
  260.         y  = (t.b + t.a) / 2;
  261.         x0 = t.c - 5;
  262.         x1 = tick[tick.length - 1].c + 5;
  263.  
  264.         g.drawLine(x0, y, x1, y);
  265.  
  266.         g.setColor(Color.gray);
  267.         g.drawLine(x1 + 1, y - 1, x0 - 1, y - 1);
  268.         g.drawLine(x0 - 1, y - 1, x0 - 1, y + 1);
  269.  
  270.         g.setColor(Color.lightGray);
  271.         g.drawLine(x0, y + 1, x1 + 1, y + 1);
  272.         g.drawLine(x1 + 1, y + 1, x1 + 1, y);
  273.  
  274.         g.setColor(Color.white);
  275.         g.drawLine(x0 - 1, y + 2, x1 + 2, y + 2);
  276.         g.drawLine(x1 + 2, y + 2, x1 + 2, y - 1);
  277.         
  278.         //???RKM??? This cannot work
  279.         g.clipRect(0, 0, width, height);
  280.         
  281.         thumb.draw(g, tick[curPos]);
  282.  
  283.         prevPos = curPos;
  284.     }
  285.  
  286.     private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
  287.         in.defaultReadObject();
  288.         
  289.         //???RKM??? I do not believe this is necessary anymore, but can't test so I'll leave it in
  290.         do_reshape(width, height);
  291.     }
  292.  
  293.     /**
  294.      * Length of the gauge ticks in pixels.
  295.      */
  296.     protected static final int TICK_HEIGHT  = 4;
  297.  
  298.     /**
  299.      * The command name of the action event fired by this component.
  300.      */
  301.      protected String actionCommand = "HSliderMoved";
  302.  
  303.     private HorizontalSliderThumb thumb;
  304.     private Mouse        mouse            = null;
  305.     private MouseMtn    mouseMotion        = null;
  306.  
  307. }
  308.  
  309. abstract class HorizontalSliderThumb implements java.io.Serializable
  310. {
  311.     void resize(int width, int height)
  312.     {
  313.         x = (this.width = width) / 2;
  314.         y = (this.height = height) - HorizontalSlider.TICK_HEIGHT - 1 - 1;
  315.     }
  316.  
  317.     abstract void draw(Graphics g, SliderTick t);
  318.  
  319.     protected void draw(int x0, int y0, int x1, int y1)
  320.     {
  321.         g.drawLine(t.c + x0, t.a + y0 + HorizontalSlider.TICK_HEIGHT + 1,
  322.                    t.c + x1, t.a + y1 + HorizontalSlider.TICK_HEIGHT + 1);
  323.     }
  324.  
  325.     protected void initDraw(Graphics g, SliderTick t)
  326.     {
  327.         this.g = g;
  328.         this.t = t;
  329.  
  330.         g.setColor(Color.lightGray);
  331.         g.fillRect(t.c - x + 1, t.a + 2 + HorizontalSlider.TICK_HEIGHT + 1, width - 2, y - 2);
  332.         
  333.         g.setColor(Color.white);
  334.     }
  335.  
  336.     void clip(Graphics g, SliderTick t)
  337.     {
  338.         g.clipRect(t.c - width / 2, t.a, width + 1, height + 1);
  339.     }
  340.  
  341.     protected int x;
  342.     protected int y;
  343.     protected int width;
  344.     protected int height;
  345.     transient protected Graphics g;
  346.     transient protected SliderTick t;
  347. }
  348.  
  349.  
  350. class HorizontalSliderThumbBoth extends HorizontalSliderThumb
  351. {
  352.     void draw(Graphics g, SliderTick t)
  353.     {
  354.         super.initDraw(g, t);
  355.  
  356.         draw(-x, y, -x, 1);
  357.         draw(-x, 1, x, 1);
  358.  
  359.         g.setColor(Color.black);
  360.         draw(-x, y, x, y);
  361.         draw(x, y, x, 1);
  362.  
  363.         g.setColor(Color.gray);
  364.         draw(1 - x, y - 1, x - 1, y - 1);
  365.         draw(x - 1, y - 1, x - 1, 2);
  366.     }
  367. }
  368.  
  369.  
  370. class HorizontalSliderThumbTop extends HorizontalSliderThumb
  371. {
  372.     void draw(Graphics g, SliderTick t)
  373.     {
  374.         super.initDraw(g, t);
  375.  
  376.         int a = y / 5;
  377.         
  378.         draw(-x, y, -x, a);
  379.         draw(-x, a, 0, 1);
  380.  
  381.         g.setColor(Color.black);
  382.         draw(0, 1, x, a);
  383.         draw(x, a, x, y);
  384.         draw(x, y, -x, y);
  385.  
  386.         g.setColor(Color.gray);
  387.         draw(0, 2, x - 1, a);
  388.         draw(x - 1, a, x - 1, y - 1);
  389.         draw(x - 1, y - 1, 1 - x, y - 1);
  390.     }
  391. }
  392.  
  393.  
  394. class HorizontalSliderThumbBot extends HorizontalSliderThumb
  395. {
  396.     void draw(Graphics g, SliderTick t)
  397.     {
  398.         super.initDraw(g, t);
  399.  
  400.         int a = height - HorizontalSlider.TICK_HEIGHT - 1 - 1 - y / 5;
  401.  
  402.         draw(x, 1, -x, 1);
  403.         draw(-x, 1, -x, a);
  404.         draw(-x, a, 0, y - 1);
  405.  
  406.         g.setColor(Color.black);
  407.         draw(0, y, x, a);
  408.         draw(x, a, x, 1);
  409.  
  410.         g.setColor(Color.gray);
  411.         draw(0, y - 1, x - 1, a);
  412.         draw(x - 1, a, x - 1 , 2);
  413.     }
  414. }
  415.  
  416.